<template>
  <el-dialog
    :title="!dataForm.id ? '新增岗位' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    width="550px"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      validate-on-rule-change
      label-width="120px"
    >
      <el-form-item label="闸机编号" prop="deviceNo">
        <el-input v-model="dataForm.deviceNo" placeholder="闸机编号"></el-input>
      </el-form-item>
      <el-form-item label="闸机名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="闸机名称"></el-input>
      </el-form-item>
      <el-form-item label="设备ip端口号" prop="deviceIp">
        <el-input placeholder="设备ip端口号" v-model="dataForm.deviceIp" clearable></el-input>
      </el-form-item>

      <el-form-item label="数据库密码" prop="password">
        <el-input placeholder="数据库密码" v-model="dataForm.password" clearable></el-input>
      </el-form-item>

      <el-form-item label="闸机状态">
        <el-select v-model="dataForm.status" placeholder="请选择">
          <el-option label="已连接" value="1"></el-option>
          <el-option label="已断开" value="0"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { isEmail, isMobile } from "@/utils/validate";

import { treeDataTranslate } from "@/utils";
export default {
  data() {
    return {
      visible: false,
      postType: 1,
      roleList: [],
      menuList: [],
      menuListTreeProps: {
        children: "children",
        label: "lable",
        disabled: val => val.type == 1
      },
      dataForm: {},
      dataRule: {
        name: [{ required: true, message: " 名称不能为空", trigger: "blur" }],
        deviceNo: [
          { required: true, message: "设备号不能为空", trigger: "blur" }
        ],
        deviceIp: [
          { required: true, message: "设备ip端口号不能为空", trigger: "blur" }
        ],
        databaseIp: [
          { required: true, message: "数据库ip端口号不能为空", trigger: "blur" }
        ],
        username: [
          { required: true, message: "数据库用户名不能为空", trigger: "blur" }
        ],
        password: [
          { required: true, message: "数据库密码不能为空", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    // 菜单树选中
    menuListTreeCurrentChangeHandle(data, node) {
      if (data.type == 1) {
        return false;
      }
      this.dataForm.parentId = data.value;
      this.dataForm.parentName = data.lable;
    },
    handleCheckChange(data, checked, indeterminate) {
      console.log(data, checked, indeterminate);
    },
    init(item, type) {
      this.dataForm = {};
      if (item) {
        this.dataForm = item;
      }
      this.dataForm = Object.assign({}, this.dataForm, { type: type });
      this.visible = true;
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          let data = JSON.stringify(this.dataForm);
          this.$http({
            url: this.$http.adornUrl(`/sys/gate/saveOrUpdateGate`),
            method: "post",
            data: this.$http.adornData({ data: data })
          }).then(({ data }) => {
            if (data && data.code == 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    }
  }
};
</script>
